﻿using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SimpleORM.Extensions;
using SimpleORM.Extensions.MemoryCache;
using SimpleORM.Infrastructure;
using SimpleORM.Infrastructure.Concurrency;
using SimpleORM.Model.DataBaseModel.SYSModelmain;
using SimpleORM.Model.SYSModel;
using SimpleORM.Service.IService;
using System.Reflection;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Text;
using SimpleORM.Infrastructure.GetToken;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace SimpleORM.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[ServiceFilter(typeof(SemaphoreActionFilter))]
    public class TestController : ControllerBase
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly IGetuser getuser;
        private readonly IGetuser2 getuser2;
        private readonly CacheManager cacheManager;

        public TestController(IGetuser _etuser, IGetuser2 _etuser2, CacheManager _cacheManager)
        {
            getuser = _etuser;
            getuser2 = _etuser2;
            cacheManager = _cacheManager;

        }
        /// <summary>
        /// 测试仓储接口
        /// </summary>
        /// <returns></returns>
        [ServiceFilter(typeof(SemaphoreActionFilter))]
        [HttpPost(Name = "UserName2")]
        public async Task<string> UserName2( string name ="123")
        {
            string xx = DateTime.Now.ToString();
            Console.WriteLine(xx+"\n");
            return getuser2.Getusrname();

        }

        [HttpPost(Name = "MemoryCacheXX")]
        public async Task<string> MemoryCacheXX(UserMage2 userMages)
        {
 
            string UUID=Guid.NewGuid().ToString().ToLower();

            return getuser2.Getusrname();

        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userMages"></param>
        /// <returns></returns>
        [HttpPost(Name = "LoginInAsync")]
        public async Task<string> LoginInAsync(UserMage2 userMages)
        {
            //测试滑动过期
            string UUID = Guid.NewGuid().ToString().ToLower();
            string key = "1234";

            string value = "12345";

            //object
            //cacheManager.Set_SlidingAndAbsoluteExpire(key, value,TimeSpan.FromMilliseconds(5*1000), TimeSpan.FromMilliseconds(60 * 1000));//秒
            //T泛型
            cacheManager.Set_T_SlidingAndAbsoluteExpire<string>(key, value, TimeSpan.FromMilliseconds(5 * 1000), TimeSpan.FromMilliseconds(60 * 1000));//秒


            bool xx =true;
            Console.WriteLine("输出 :" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff")+"\n");
            while (xx)
            {
                Thread.Sleep(4000);
                xx= cacheManager.Exists(key);
                string cc = true ? "OK" : "NG";
                
                Console.WriteLine("输出 :" +cc+"："+ DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff") + "\n");
               
                if(cacheManager.Exists(key))
                    cacheManager.Remove(key);
            }
            Console.WriteLine("输出 :" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff") + "\n");

            return xx == true ? "OK" : "NG";
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="userMages"></param>
        /// <returns></returns>
        [HttpPost(Name = "LoginOutAsync")]
        public async Task<string> LoginOutAsync(UserMage2 userMages)
        {
            string UUID = Guid.NewGuid().ToString().ToLower();
            TokenManagement tokenManagement = new TokenManagement();
            var xx = tokenManagement.GenerationToken("123");

            var xx2 = tokenManagement.GenerationToken("123","123");


            return getuser2.Getusrname();

        }

    }
}
